﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using Config;
using DataAccess;

namespace Manager
{
    public partial class Login : System.Web.UI.Page
    {
        int count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtUserName.Focus();
                if (Session[Constants.SESSION_USERID] != null)
                {
                    Response.Redirect("Default.aspx");
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            int result;
            string userName = txtUserName.Text.Trim();
            string password = Encrypt.Encryption.GetMD5(txtPassword.Text);
            UserBusiness userBusiness = new UserBusiness();

            result = userBusiness.CheckLogin(userName, password);

            Session[Constants.SESSION_USERID] = result;

            if (result == 0)
            {
                Session[Constants.SESSION_USERID] = null;
                lblError.Text = "Sai tên đăng nhập hoặc mật khẩu! Vui lòng đăng nhập lại.";
                count++;
                return;
            }

            if (count >= 3)
            {
                Response.Redirect("DenyPermission.aspx");
            }

            Session[Constants.SESSION_CURRENT_PAGE] = "0";
            if (Session[Constants.SESSION_CURRENT_URL] == null)
                Session[Constants.SESSION_CURRENT_URL] = "Default.aspx";
            Response.Redirect(Session[Constants.SESSION_CURRENT_URL].ToString());
        }
    }
}